<?php
/**
 * Crc CMS
 *
 * @category   Crc
 * @package    Plugins
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @copyright  Copyright (c) 2012
 * @version    $Id: Init.php 67 2013-02-22 17:15:36Z mcalin45@gmail.com $
 */


/**
 * Class for the Init plugin.
 *
 * @category   Crc
 * @package    Plugins
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @copyright  Copyright (c) 2012
 */
class Plugins_Init extends Zend_Controller_Plugin_Abstract
{
    /**
     * Constructor
     *
     * @return void
     **/
	public function __construct()
    {
        $this->_initIdentity();
	}

    /**
     * Register the logged identity
     *
     * @return void
     **/
	private function _initIdentity()
    {
        if (Zend_Auth::getInstance()->hasIdentity())
        {
            $doctrine = Zend_Registry::get('doctrine');
            $userRepository = $doctrine->getEntityManager()->getRepository('\Crc\Entity\User');
            $userLoggedEmail = Zend_Auth::getInstance()->getIdentity();
            $user = $userRepository->findOneByEmail($userLoggedEmail);
            Zend_Registry::set('userInfo', $user);
        }
	}

    /**
     * preDispatch
     *
     * If the request is for the admin section change the layout accordingly
     *
     * @param Zend_Controller_Request_Abstract $request
     * @return void
     **/
	public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $layout = Zend_Layout::getMvcInstance();
        if ($request->getControllerName() == "admin" )
        {
            $layout->setLayout("admin");
        }
	}
}